package cn.etgps.etgps.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.etgps.etgps.R;
import cn.etgps.etgps.utils.MainUsedInterface;


public class TitleBar extends LinearLayout implements OnClickListener {
    private boolean isShowSearchView = false;//是否显示搜索框
    private TextView mTitle = null;//标题
    private SearchView mSearchView = null;//搜索框
    private Button mLeftTextBtn = null;//左标题按钮
    private Button mRightTextBtn = null;//右标题按钮
    private ImageButton mLeftImageBtn = null;//左图片按钮
    private ImageButton mRightImageBtn = null;//右图片按钮
    private MainUsedInterface.MainButtonClickListener mMainClickListener = null;//按键监听
    private String mLeftButtonName = "";//左按钮内容
    private String mRightButtonName = "";//右按钮内容

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initUI(context);
    }

    public TitleBar(Context context) {
        super(context);
        initUI(context);
    }

    //点击监听，用类实现MainButtonClickListener接口作为监听者
    public void setMainClickListener(MainUsedInterface.MainButtonClickListener listener, String leftButtonName,
            String rightButtonName) {
        mMainClickListener = listener;
        mLeftButtonName = leftButtonName;
        mRightButtonName = rightButtonName;
    }

    //解析布局
    private void initUI(Context context) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View view = mInflater.inflate(R.layout.titlebar_view, null);
        this.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mSearchView = (SearchView) view.findViewById(R.id.search_group);
        mTitle = (TextView) view.findViewById(R.id.title);
        mLeftTextBtn = (Button) view.findViewById(R.id.left_text_btn);
        mRightTextBtn = (Button) view.findViewById(R.id.right_text_btn);
        mLeftImageBtn = (ImageButton) view.findViewById(R.id.left_image_btn);
        mRightImageBtn = (ImageButton) view.findViewById(R.id.right_image_btn);
    }

    public SearchView getSearchView() {
        return mSearchView;
    }

    //初始化控件，标题、左侧文字，右侧文字，搜索栏是否显示
    public void initTitleBarInfo(String title, String leftBtnText, String rightBtnText, boolean isShowSearch) {
        mTitle.setText(title);
        isShowSearchView = isShowSearch;
        if (isShowSearchView) {
            mSearchView.setVisibility(View.VISIBLE);
            mTitle.setVisibility(View.GONE);
        } else {
            mSearchView.setVisibility(View.GONE);
            mTitle.setVisibility(View.VISIBLE);
        }
        if (!leftBtnText.equals("")) {
            mLeftTextBtn.setText(leftBtnText);
            mLeftTextBtn.setVisibility(View.VISIBLE);
            if (mLeftTextBtn.getText().toString().length() > 2) {
                mLeftTextBtn.setPadding(0, 0, 16, 0);
            }
            mLeftTextBtn.setOnClickListener(this);
        } else {
            mLeftTextBtn.setText("");
            mLeftTextBtn.setVisibility(View.GONE);
        }

        if (!rightBtnText.equals("")) {
            mRightTextBtn.setText(rightBtnText);
            mRightTextBtn.setVisibility(View.VISIBLE);
            mRightTextBtn.setOnClickListener(this);
        } else {
            mRightTextBtn.setText("");
            mRightTextBtn.setVisibility(View.GONE);
        }
    }

    //初始化标题
    public void initTitleBarInfo(String title, String leftBtnText, int leftImageId, String rightBtnText,
            int rightImageId, boolean isShowSearch) {
        mTitle.setText(title);
        isShowSearchView = isShowSearch;
        if (isShowSearchView) {
            mSearchView.setVisibility(View.VISIBLE);
            mTitle.setVisibility(View.GONE);
        } else {
            mSearchView.setVisibility(View.GONE);
            mTitle.setVisibility(View.VISIBLE);
        }
        if (!leftBtnText.equals("")) {
            mLeftTextBtn.setText(leftBtnText);
            mLeftTextBtn.setVisibility(View.VISIBLE);
            mLeftImageBtn.setVisibility(View.GONE);
            mLeftTextBtn.setOnClickListener(this);
            if (mLeftTextBtn.getText().toString().length() > 2) {
                mLeftTextBtn.setPadding(0, 0, 16, 0);//padding-bottom=16
            }
        } else if (leftImageId > 0) {
            mLeftTextBtn.setText("");
            mLeftTextBtn.setVisibility(View.GONE);
            mLeftImageBtn.setVisibility(View.VISIBLE);
            mLeftImageBtn.setBackgroundResource(leftImageId);
            mLeftImageBtn.setOnClickListener(this);
        } else {
            mLeftTextBtn.setText("");
            mLeftTextBtn.setVisibility(View.GONE);
            mLeftImageBtn.setVisibility(View.GONE);
        }


        if (!rightBtnText.equals("")) {
            mRightTextBtn.setText(rightBtnText);
            mRightTextBtn.setVisibility(View.VISIBLE);
            mRightImageBtn.setVisibility(View.GONE);
            mRightTextBtn.setOnClickListener(this);
        } else if (rightImageId > 0) {
            mRightTextBtn.setText("");
            mRightTextBtn.setVisibility(View.GONE);
            mRightImageBtn.setVisibility(View.VISIBLE);
            mRightImageBtn.setBackgroundResource(rightImageId);
            mRightImageBtn.setOnClickListener(this);
        } else {
            mRightTextBtn.setText("");
            mRightTextBtn.setVisibility(View.GONE);
            mRightImageBtn.setVisibility(View.GONE);
        }
    }

    public void updateTitle(String newTitle) {
        mTitle.setText(newTitle);
    }

    public String getTitle() {
        return mTitle.getText().toString();
    }

    @Override
    public void onClick(View v) {
        if (mMainClickListener == null) {
            return;
        }
        if (v.getId() == R.id.left_image_btn || v.getId() == R.id.left_text_btn) {
            // Left button clicked
            mMainClickListener.clickButtonListener(mLeftButtonName);
        } else {
            // Right button clicked
            mMainClickListener.clickButtonListener(mRightButtonName);
        }
    }
    /**
     * 执行构造函数后，执行setMainClickListener（listener,leftButtonName,rightButtonName）
     * 以及initTitleBarInfo(title,leftbtn/leftImg,rightbtn/rightImg,showSearchView)
     */
}
